@rbxts/jsnatives



Proxy - apply hook

The apply hook is triggered when a proxy is called as a function.

Signature

function apply<T>(target: T, proxy: T, ...args: unknown[]): any;

Description

The apply handler trap is called when the proxy is invoked as a function. It allows you to intercept function call operations.

Parameters

Return value

Examples

Basic function call interception

const target = function(x, y) {
return x + y;
};
const proxy = new Proxy(target, {
apply: (target, proxy, ...args) => {
console.log(`Function called with arguments: ${args.join(", ")}`);
return target(...args);
}
});
console.log(proxy(1, 2)); // Logs: "Function called with arguments: 1, 2", then 3

Function call transformation

const multiply = function(x, y) {
return x * y;
};
const proxy = new Proxy(multiply, {
apply: (target, proxy, ...args) => {
// Double the result
return target(...args) * 2;
}
});
console.log(proxy(2, 3)); // 12 (2 * 3 * 2)

Function call validation

const divide = function(x, y) {
return x / y;
};
const safeDivide = new Proxy(divide, {
apply: (target, proxy, ...args) => {
if (args[1] === 0) {
throw new Error("Cannot divide by zero");
}
return target(...args);
}
});
console.log(safeDivide(10, 2)); // 5
// safeDivide(10, 0); // Throws: "Cannot divide by zero"

Method profiling

const calculator = {
add: function(x, y) { return x + y; },
subtract: function(x, y) { return x - y; },
multiply: function(x, y) { return x * y; }
};
const profiledCalculator = new Proxy(calculator, {
get: (target, prop, proxy) => {
const value = target[prop];
if (typeof value === "function") {
return new Proxy(value, {
apply: (fn, thisArg, ...args) => {
console.time(`${String(prop)} operation`);
const result = fn.apply(target, args[0]);
console.timeEnd(`${String(prop)} operation`);
return result;
}
});
}
return value;
}
});
profiledCalculator.add(1, 2); // Logs: "add operation: 0.123ms"
profiledCalculator.multiply(3, 4); // Logs: "multiply operation: 0.045ms"